package cn.tiancaifan.micro.service;


import cn.tiancaifan.micro.entity.Hr;
import cn.tiancaifan.micro.entity.Menu;
import cn.tiancaifan.micro.mapper.MenuMapper;
import cn.tiancaifan.micro.mapper.MenuRoleMapper;
import cn.tiancaifan.micro.utils.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;

    public List<Menu> getMenusByHrId() {
        return menuMapper.getMenusByHrId(((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }

    @Cacheable
    public List<Menu> getMenusWithRole(){
        return menuMapper.getMenusWithRole();
    }

    @Transactional
    @Logger("修改用户可访问信息")
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if (mids == null || mids.length == 0) {
            return true;
        }
        Integer result = menuRoleMapper.insertRecord(rid, mids);
        return result==mids.length;
    }
}
